<?php
/**
 * @todo 安装程序
 * @copyright gaoxiang 2009
 * @author gaoxiang
 * @createdate 2009.03.17
 * @license 本程序遵照Creative Commons 署名-非商业性使用-相同方式共享 2.5 中国大陆 协议，该协议的中文版本可在http://creativecommons.org/licenses/by-nc-sa/2.5/cn/下查看。
 **/
require_once('config.php');
require_once("require/function.php");
/** 安装页头信息 **/
function install_header()
{
	$head_echo = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title>金龙集团铝管事业部数据系统安装</title>
	<meta name="description" content="金龙集团铝管事业部数据系统安装" />
	<style type="text/css">
		* {margin:0 auto;font-family: Consolas Georgia;color:#000000;font-size: 18px;}
		#body { background: #FFFFFF;}
		#page {width: 970px;margin:0 auto;padding: 0 15px 50px 15px;background: #F7F7F7;border-style : solid; border-width: 1px;border-color : #5c5c5c;}
		#title {padding:10px 0 10px 5px;font-size: 200%;}
		#small {font-size: 12px;color: #666666;}
		#form {margin: 100px auto; }
		#logo {width: 970px;height: 120px;background: #f7f7f7 url("images/logo.jpg");margin:40px auto;}
		.button {
			text-decoration: none;
			line-height: 16px;
			padding: 6px 12px;
			cursor: pointer;
			border: 1px solid #bbb;
			color: #464646;
			-moz-border-radius: 15px;
			-khtml-border-radius: 15px;
			-webkit-border-radius: 15px;
			border-radius: 15px;
			-moz-box-sizing: content-box;
			-webkit-box-sizing: content-box;
			-khtml-box-sizing: content-box;
			box-sizing: content-box;
		}
		#foot {margin: 10px auto;font-size: 14px;text-align:center;}
		</style>	
 </head><body id="body" onload="startclock()"><div id="page"><div id="logo"></div><div id="title">安装</div>';
echo $head_echo;
}
if($_GET['step']== "second"){
		install_header();
		$link=mysql_connect($db_config['host'],$db_config['user'],$db_config['pass']) or die ('服务器连接失败: ' . mysql_error()); ?>
		<div style="margin:50px 0 0 50px;">
		<?php 
		if(mysql_select_db('gdcopper_al', $link)){
					echo "欢迎使用金龙集团铝管事业部数据系统！但是你可能已经安装此系统!</div>";
					html_footer();
		}else{
			if(!mysql_query("CREATE DATABASE `gdcopper_al` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci")){
				echo "创建数据库失败，请检查连接！</div>";
				html_footer();
				exit;
			}
			if(!mysql_select_db('gdcopper_al', $link)){
					echo "数据库连接失败，请检查网络连接!</div>";
					mysql_query("DROP TABLE [IF EXISTS] 'gdcopper_al'");
					html_footer();
					exit;
			}
			$create_produce = "CREATE TABLE produce (pid INT NOT NULL AUTO_INCREMENT PRIMARY KEY, palter_time TIMESTAMP(12) ,pcreate_time DATETIME,pclient char(30),pspec char(10),prog CHAR(12),pmachine CHAR(2),pclass CHAR(1),pgroup CHAR(1),product_id CHAR(12),pmould char(3) NOT NULL DEFAULT 'N/A' ,pweight DOUBLE(7,1) NOT NULL DEFAULT 0,pdefect CHAR(3) NOT NULL DEFAULT 'N/A',psurface char(30),pjudge CHAR(8),premarks TEXT,cuserid INT,uuserid INT,UNIQUE(product_id),INDEX(prog,palter_time,pcreate_time,pclient,pmould,pspec,pclass,pgroup,pjudge,cuserid,uuserid))";
			$create_produce_undo = "CREATE TABLE produce_undo (pid INT NOT NULL AUTO_INCREMENT PRIMARY KEY, palter_time TIMESTAMP(12) ,pcreate_time DATETIME,pclient char(30),pspec char(10),prog CHAR(12),pmachine CHAR(2),pclass CHAR(1),pgroup CHAR(1),product_id CHAR(12),pmould char(3) NOT NULL DEFAULT 'N/A' ,pweight DOUBLE(7,1) NOT NULL DEFAULT 0,pdefect CHAR(3) NOT NULL DEFAULT 'N/A',psurface char(30),pjudge CHAR(8),premarks TEXT,cuserid INT,uuserid INT,UNIQUE(product_id),INDEX(prog,palter_time,pcreate_time,pclient,pmould,pspec,pclass,pgroup,pjudge,cuserid,uuserid))";
			$create_config = "CREATE TABLE config (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,field char(40),value TEXT,UNIQUE(field))";
			$create_user = "CREATE TABLE users (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,rights CHAR(20),user char(20),pass char(32),UNIQUE(user),INDEX(rights,pass))";
			$create_dis = "CREATE TABLE disqualification (disid INT AUTO_INCREMENT PRIMARY KEY,disupdatetime TIMESTAMP(12),discreatedate DATE,disproduct CHAR(10),disclass CHAR(1),disgroup CHAR(1),dismachine CHAR(2),dispremium DOUBLE(7,1),cuserid INT,uuserid INT,INDEX(discreatedate,disclass,disgroup,dismachine))";
			$insert_admin = "INSERT INTO users VALUES ('','1111111111','budgerigar','262fca80dc9261a9f1cbe31b2754b40f')";
			$specialpass="CREATE TABLE specialpass (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, createtime TIMESTAMP(12),pass CHAR(32),creater CHAR(20),INDEX(createtime))";
			$pack="CREATE TABLE pack (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,create_time DATETIME,alter_time TIMESTAMP(12),product CHAR(12),process CHAR(12),judge CHAR(12),remarks TEXT,cuserid INT,uuserid INT,UNIQUE(product),INDEX(cuserid))";
			$rog="CREATE TABLE rog (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,create_time DATETIME,alter_time	TIMESTAMP(12),stockdate DATE,supplier char(1),make char(4),spec double(3,1),weight double(7,1),origin char(20),raw char(20),tensile double(4,1),extend double(4,1),anneal char(20),technics char(20),atensile double(4,1),aextend double(4,1),judge CHAR(10),remarks text,cuserid int,uuserid int,UNIQUE(origin,raw,anneal),index(create_time,alter_time,stockdate,supplier,make,spec,weight,tensile,extend,atensile,aextend,cuserid,judge))";
			if(!mysql_query($create_produce)){
				echo "创建produce表失败，请检查连接！</div>";
					mysql_query("DROP TABLE [IF EXISTS] 'gdcopper_al'");
				html_footer();
				exit;
			}
			if(!mysql_query($create_produce_undo)){
				echo "创建produce_undo表失败，请检查连接！</div>";
					mysql_query("DROP TABLE [IF EXISTS] 'gdcopper_al'");
				html_footer();
				exit;
			}
			if(!mysql_query($create_config)){
				echo "创建config表失败，请检查连接！</div>";
					mysql_query("DROP TABLE [IF EXISTS] 'gdcopper_al'");
				html_footer();
				exit;
			}
			if(!mysql_query($create_user)){
				echo "创建user表失败，请检查连接！</div>";
					mysql_query("DROP TABLE [IF EXISTS] 'gdcopper_al'");
				html_footer();
				exit;
			}
			if(!mysql_query($create_dis)){
				echo "创建disqualification表失败，请检查连接！</div>";
					mysql_query("DROP TABLE [IF EXISTS] 'gdcopper_al'");
				html_footer();
				exit;
			}
			if(!mysql_query($insert_admin)){
				echo "插入用户数据失败，请检查连接！</div>";
					mysql_query("DROP TABLE [IF EXISTS] 'gdcopper_al'");
				html_footer();
				exit;
			}
			if(!mysql_query($pack)){
				echo "创建pack表失败，请检查连接！</div>";
					mysql_query("DROP TABLE [IF EXISTS] 'gdcopper_al'");
				html_footer();
				exit;
			}
			if(!mysql_query($rog)){
				echo "创建rog表失败，请检查连接！</div>";
					mysql_query("DROP TABLE [IF EXISTS] 'gdcopper_al'");
				html_footer();
				exit;
			}
			?>
				安装成功，请进入首页！</div>
		<center><div id="form">
			<form action="install.php?step=successful" method="POST">
				<button type="submit" class="button" />进入首页</button>
			</form></div></center>
			<?php 
				html_footer();
			} 
}elseif($_GET['step']=="successful"){
		require_once('require/class.php');
		$url_this =  "http://" . (isset($_SERVER['HTTP_X_FORWARDED_HOST'])?$_SERVER['HTTP_X_FORWARDED_HOST']:(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '')) . implode('/',explode('/',$_SERVER['PHP_SELF'],-1)) . '/';
		$insert_url = "INSERT INTO config VALUES('','site_url',\"$url_this\")";
		$database->write_table($insert_url);
		header("Location: $url_this");
		page_echo();
		html_footer();
}else{
				install_header();?>
				<div style="margin:50px 0 0 50px;">欢迎使用金龙集团铝管事业部数据系统！请确认config.php文件正确编辑后点击安装。</div>
				<center><div id="form">
				<form action="install.php?step=second" method="POST">
						<p><button type="submit" name="install" class="button">安装</button></p>
				</form></div></center>
				<?php 
				html_footer();
}
?>